package cn.sumpu.app.guoguojie.web.mvc.family;


import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.sumpu.app.guoguojie.biz.FriendBiz;
import cn.sumpu.app.guoguojie.biz.MessageBiz;
import cn.sumpu.app.guoguojie.constant.MessageType;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Message;
import cn.sumpu.app.guoguojie.domain.Ext.PageModel;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;

@Controller
public class MessageController {

	@Autowired
	private MessageBiz messageBiz;
	@Autowired
	private FriendBiz friendBiz;

	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/message.htm", method = RequestMethod.GET)
	public String message(HttpServletRequest request) {
		return "message/message";
	}

	/**
	 * 邻居邀请消息页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/messageFriend.htm", method = RequestMethod.GET)
	public String messageFriend(HttpServletRequest request) {
		Member member = (Member) request.getSession().getAttribute("member");
		String page = request.getParameter("page");
		if(Common.empty(page)){
			page="1";
		}
		try {
			messageBiz.readMessageFriend(member.getFamily());
			int messageFriendNoRead = messageBiz.messageFriendNoReadCount(member.getFamily());
			int messageInnerNoRead = messageBiz.messageInnerNoReadCount(member.getFamily());
			request.setAttribute("messageFriendNoRead", messageFriendNoRead);
			request.setAttribute("messageInnerNoRead", messageInnerNoRead);
			
			request.setAttribute("messageNoRead", Common.getCountFormat(messageFriendNoRead+messageInnerNoRead));
			
			PageModel pageModel =	messageBiz.getMessageFriend(member.getFamily(), Integer.parseInt(page));
			request.setAttribute("pageModel", pageModel);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "message/messageFriend";
	}
	
	/**
	 * 同意好友请求，默认加入我的朋友分组
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/agreeFriend.htm", method = RequestMethod.POST)
	public String agreeFriend(String msgId,String familyId,HttpServletRequest request,HttpServletResponse response) {
		Member member = (Member) request.getSession().getAttribute("member");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			friendBiz.agreeFriend(member, familyId,msgId);
			pw.print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 忽略好友请求，默认加入我的朋友分组
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/ignoreFriend.htm", method = RequestMethod.POST)
	public String ignoreFriend(String msgId,String familyId,HttpServletRequest request,HttpServletResponse response) {
		Member member = (Member) request.getSession().getAttribute("member");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			friendBiz.ignoreFriend(member, familyId,msgId);
			pw.print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 他家页面发送消息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sendMessageFamily.htm")
	public String sendMessageFamily(String family,String msgContent,HttpServletRequest request,HttpServletResponse response) {
		Member member = (Member) request.getSession().getAttribute("member");
		response.setContentType("text/plain; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			Message message = new Message();
			message.setSendMember(member.getIdentifier());
			message.setReceiveFamily(family);
			message.setType(MessageType.INNER.getValue());
			message.setContent(msgContent);
			message.setIsRead(false);
			messageBiz.insertMessage(member,message);
			pw.print("success");
			
		} catch (Exception e) {
			e.printStackTrace();
			pw.print("error");
		}
		return  null;
	}
	
	
	/**
	 * 站内消息页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/messageInner.htm")
	public String messageInner(HttpServletRequest request) {
		Member member = (Member) request.getSession().getAttribute("member");
		String page = request.getParameter("page");
		if(Common.empty(page)){
			page="1";
		}
		try {
			messageBiz.readMessageInner(member.getFamily());
			int messageFriendNoRead = messageBiz.messageFriendNoReadCount(member.getFamily());
			int messageInnerNoRead = messageBiz.messageInnerNoReadCount(member.getFamily());
			request.setAttribute("messageFriendNoRead", messageFriendNoRead);
			request.setAttribute("messageInnerNoRead", messageInnerNoRead);
			request.setAttribute("messageNoRead", Common.getCountFormat(messageFriendNoRead+messageInnerNoRead));
			
			PageModel pageModel =	messageBiz.getMessageInner(member.getIdentifier(),member.getFamily(), Integer.parseInt(page));
			request.setAttribute("pageModel", pageModel);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "message/messageInner";
	}
	
	/**
	 * 删除消息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/messageInnerDelete.htm")
	public String messageInnerDelete(String messageId,HttpServletRequest request) {
		
		try {
			messageBiz.deleteMessageById(messageId);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return messageInner(request);
	}
	
	/**
	 * 回复消息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/replyMessage.htm")
	public String replyMessage(String receiveFamily,String content,String replyId,HttpServletRequest request) {
		Member member = (Member) request.getSession().getAttribute("member");
		try {
			messageBiz.replyMessage(member, receiveFamily, content, replyId);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return messageInner(request);
	}

}
